package com.young.netty.gateway.gateway;

import com.young.netty.gateway.context.BackendContext;
import com.young.netty.gateway.context.FrontContext;
import com.young.netty.gateway.context.FullHttpContext;
import com.young.netty.gateway.context.HttpContext;
import com.young.netty.gateway.interceptor.InterceptorChain;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Young
 * @Date 2021-07-24 19:38
 */
@Slf4j
public class NettyNioServerHandler extends ChannelInboundHandlerAdapter {

    private InterceptorChain interceptorChain;

    public NettyNioServerHandler(InterceptorChain interceptorChain) {
        this.interceptorChain = interceptorChain;
    }

    /**
     * 读取数据
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        FrontContext frontContext = parseFrontContext((FullHttpRequest) msg);
        BackendContext backendContext = new BackendContext();
        HttpContext fullHttpContext = new FullHttpContext(frontContext, backendContext);
        interceptorChain.execute(fullHttpContext);
        FullHttpResponse response = getFullHttpResponse(frontContext);
        // 消息响应完成后即断开连接
        ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    }

    /**
     * 数据读取完毕的处理
     */
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        log.info("server 读取数据完成");
    }

    /**
     * 出现异常的处理
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        log.error("server 读取数据出现异常", cause);
        ctx.close();
    }

    /**
     * 解析前端上下文
     *
     * @param fullHttpRequest
     * @return
     */
    private FrontContext parseFrontContext(FullHttpRequest fullHttpRequest) {
        FrontContext frontContext = new FrontContext();
        frontContext.setUrl(frontContext.getUrl());
        frontContext.setMethod(fullHttpRequest.method());
        frontContext.setRequestHeaders(fullHttpRequest.headers());
        frontContext.setRequestContent(fullHttpRequest.content());
        return frontContext;
    }

    /**
     * 前端响应
     *
     * @param frontContext
     * @return
     */
    private FullHttpResponse getFullHttpResponse(FrontContext frontContext) {
        FullHttpResponse response = new DefaultFullHttpResponse(
                HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
                frontContext.getResponseByteBuf());
        response.headers().add(frontContext.getResponseHeaders());
        response.headers().setInt("Content-Length",
                response.content().readableBytes());
        return response;
    }

}
